package echecs;

public class Regle extends Pred {

    private Direction fC = Direction.FinCavalier;
    private Direction f = Direction.Fin;

    public Regle(Case c, APiece p) {
        super();
        this.p = p;
        position = p.gen.getPosition();
        ListeCoups = p.gen.getListecoups();
        this._case = c;

    }

    public void ajouter() {
        ListeCoups.add(new Coups(p, p._case, _case));
    }

    public void direction(Direction dir) {

        increment(dir);
        if (predicat(_case, Case.HorsEchquier)) {
            if (predicat(_case, position)) {
                ajouter();
                if (isTypeNCaseDTF()) {
                    direction(dir);
                }

            } else {
                piece = position.getPiece(_case);
                if (estCouleurInverse(p, piece)) {
                    if (p.regletype.isTypeRoi()) {
                        if (!isEchec()) {
                            ajouter();
                        }
                    } else {
                        ajouter();
                    }
                }
            }
        }
    }

    public boolean estCouleurInverse(APiece p1, APiece p2) {
        return p1.estCouleurInverse(p2);
    }

    public void increment(Direction dir) {
        _case = _case.plus(dir);
    }

    @Override
    public boolean test() {
        return predicat(p, Piece.Cavalier_Noir) ? tclos() : fclos();
    }

    @Override
    public boolean fclos() {
        increment();
        return predicat(d, f) ? n(d) : true;

    }

    @Override
    public boolean tclos() {
        sC();
        return predicat(dC, fC) ? n(dC) : true;
    }

    public boolean isEchec() {
        return false;
    }

    public Case getCase() {
        return _case;
    }

    public Position getPosition() {
        return position;
    }
}
